﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Silvermoon.OpenGL.Native.Fonts
{
    internal static class FontUtil
    {
        public struct FontData
        {
            public GlFontFamily Family;
            public int Size;
            public FontStyle Style;
            public bool Shadowed;
        }

        public static int GetFontKey(GlFontFamily family, int size, FontStyle style, bool shadowed)
        {
            if (size < 4 || size >= 64) throw new ArgumentOutOfRangeException("size");
            int iFamily = (int)family;
            int iStyle = (int)style;

            int key = (iFamily << 10) | (size << 4) | iStyle;

            key = shadowed ? (key | 0x80000) : (key & 0x7FFFF);
            return key;
        }

        public static FontData FromFontKey(int key)
        {
            FontData data = new FontData();
            data.Shadowed = (key & 0x80000) != 0;
            key &= 0x7FFFF;
            data.Family = (GlFontFamily)(key >> 10);
            data.Size = (key >> 4) & 63;
            data.Style = (FontStyle)(key & 15);
            return data;
        }

        public static Font FromData(GlFontFamily family, int size, FontStyle style)
        {
            FontFamily ff = GetFontFamily(family);
            var font= new Font(ff, (float)size, style);
            return font;
        }


        private static FontFamily GetFontFamily(GlFontFamily family)
        {
            switch (family)
            {
                case GlFontFamily.GenericMonospace: return FontFamily.GenericMonospace;
                case GlFontFamily.GenericSansSerif: return FontFamily.GenericSansSerif;
                case GlFontFamily.GenericSerif: return FontFamily.GenericSerif;
                default: throw new NotImplementedException();
            }
        }
    }
}
